﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using PriceCacheDownloader;
using RoomPriceEntity;
using RedisDBBiz;

namespace Redis_DataLoader
{
    public static class ExecAgodaRequest
    {
        public static string AgodaUrl = "http://cxml.agoda.com/xmlpartner/XmlService.svc/search_lrv2";
        //public static Converter cv = new Converter();
        public static RoomPriceGetRedis rs = new RoomPriceGetRedis();

        public static void CheckIfAgodaHasMultiPrice(string hotelCode)
        {
            
            Interlocked.Increment(ref ExecMain.DoneHotelCount);
            int min_los = 1;
            int endLos = 7;
            bool hasResult = false;
            while (min_los <= endLos)
            {
                try
                {
                    RequestCreater rc = new RequestCreater(hotelCode);
                    DateTime dtTestDate = new DateTime(2015, 3, 18);
                    rc.SetArrival(dtTestDate);
                    rc.SetDeparture(dtTestDate.AddDays(min_los));
                    string AgodaRequestor = rc.GetAgodaRequestXml();
                    string rsGetHotel = Requestor.DoRequest(AgodaRequestor, AgodaUrl);

                    if (rsGetHotel.IndexOf(@"status=""204""") >= 0)
                    {
                        min_los++;
                        hasResult = false;
                    }
                    else
                    {
                        rs.AddItemToList("Min_LOS:Agoda:" + min_los, hotelCode);
                        hasResult = true;
                        break;
                    }
                }
                catch
                {
                    min_los++;
                }
            }
            if (!hasResult)
            {
                rs.AddItemToList("Min_LOS_AgodaOver7:" + min_los, hotelCode);
            }
        }
    }
}
